//
//  iChibiViewController.m
//  iChibi
//
//  Created by Meng Zhang on 11-5-19.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "iChibiViewController.h"

@interface iChibiViewController()
- (void)updateContent:(NSString *)content;
@end

@implementation iChibiViewController

- (void)dealloc
{
  [super dealloc];
}

- (void)didReceiveMemoryWarning
{
  // Releases the view if it doesn't have a superview.
  [super didReceiveMemoryWarning];
  
  // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
  [super viewDidLoad];
  
  //load chibi
  _chibi = [[Chibi alloc] init];
  [_chibi repl];
  
  //Configure TextField
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillShown:)
                                               name:UIKeyboardWillShowNotification object:self.view.window];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardWillBeHidden:)
                                               name:UIKeyboardWillHideNotification object:self.view.window];
  
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(chibiDidUpdate:) 
                                               name:CHIBI_UDPATE_NOTIFICATION 
                                             object:_chibi];
  
  _text.inputAccessoryView = _accessoryView;
}

- (void)viewDidUnload
{
  [super viewDidUnload];
  // Release any retained subviews of the main view.
  // e.g. self.myOutlet = nil;
  [_chibi release];
  
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  // Return YES for supported orientations
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  NSString *text = [NSString stringWithFormat:@"%@\n", textField.text];
  [_chibi sendChars:text];
  [self updateContent:text];

  textField.text = @"";
  return NO;
}

- (void)updateContent:(NSString *)content
{
  _output.text = [NSString stringWithFormat:@"%@%@", _output.text, content];
  [_output scrollRangeToVisible:NSMakeRange(_output.text.length, 0)];
}

#pragma mark - Notification
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillShown:(NSNotification*)aNotification
{
  NSDictionary* info = [aNotification userInfo];
  CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  
  CGRect output = _output.frame, input = _text.frame;
  output.size.height -= kbSize.height;
  
  input.origin.y -= kbSize.height;
  [UIView animateWithDuration:0.3 animations:^{
    _output.frame = output;
    _text.frame = input;
  }];
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
  NSDictionary* info = [aNotification userInfo];
  CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
  
  CGRect output = _output.frame, input = _text.frame;
  output.size.height += kbSize.height;
  
  input.origin.y += kbSize.height;
  [UIView animateWithDuration:0.3 animations:^{
    _output.frame = output;
    _text.frame = input;
  }];  
}

// Called when Chibi update
- (void)chibiDidUpdate:(NSNotification*)aNotifiaction
{
  NSDictionary *info = [aNotifiaction userInfo];
  NSString *content = [info objectForKey:@"content"];
  [self updateContent:content];
}

#pragma mark - Accessory View
- (IBAction)accessoryTapped:(id)sender
{
  UIButton *button = sender;
  
  UIPasteboard* lPasteBoard = [UIPasteboard generalPasteboard];
  
  // Save the current pasteboard contents so we can restore them later
  NSArray* lPasteBoardItems = [lPasteBoard.items copy];
  
  // Update the system pasteboard with my string
  lPasteBoard.string = button.titleLabel.text;
  
  // Paste the pasteboard contents at current cursor location
  [_text paste:self];
  
  // Restore original pasteboard contents
  lPasteBoard.items = lPasteBoardItems;
  
  [lPasteBoardItems release];
}
@end
